package com.example.tests;

import org.testng.annotations.Test;
import org.testng.AssertJUnit;
import static org.testng.AssertJUnit.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.testng.AssertJUnit.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.hamcrest.core.IsEqual;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class GroupModificationTests extends TestBase {
	
	Random rnd = new Random();

	@DataProvider(name = "randomGroups")
	public Iterator<Object[]> generateRandomGroups(){
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++) {
			GroupObject group = new GroupObject()
			.setName("name modded"+rnd.nextInt())
			.setHeader("header modded"+rnd.nextInt())
			.setFooter("footer modded"+rnd.nextInt());
			list.add(new Object[]{group});
		} 
		return list.iterator();
	}
	
	
	@Test (dataProvider = "randomGroups")
	public void testFirstGroupCanBeModified(GroupObject validGroup) throws Exception {
		Set<GroupObject> oldGroupList = app.getGroupHelper().getGroups();
		if (oldGroupList.size() == 0) {
			new Exception ("No groups, nothing to delete.");
		} else {
			app.getGroupHelper().modifyGroup(-1, validGroup);
		}
		Set<GroupObject> modifiedGroupList = app.getGroupHelper().getGroups();
		verifyGroupModified(oldGroupList, validGroup, modifiedGroupList);
		}
		
	private void verifyGroupModified(Set<GroupObject> oldGroupList,
			GroupObject validGroup, Set<GroupObject> modifiedGroupList ) {
		assertEquals(oldGroupList.size(), modifiedGroupList.size());
		validGroup.setId("$$$");
		validGroup.setId(null);
		AssertJUnit.assertNotSame(oldGroupList, modifiedGroupList);
	}
}
 